CC = gcc
RM = rm -rf
FLAGS = -g -o
OBJGEN = target

# 获取当前目录下的所有c文件
SRC = $(wildcard *.c)

# 将所有的.c换成.o
OBJS = $(patsubst %.c, %.o, $(SRC))
FILES = $(patsubst %.c, %.d, $(SRC))

$(OBJGEN) : $(OBJS)
	$(CC) $(FLAGS) $@ $^

%.o : %.c 
	$(CC) -c $(FLAGS) $@ $<

-include $(FILES)

%.d: %.c
	set -e; rm -f $@; \
	$(CC) -MM $(CPPFLAGS) $< > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$
 

.PHONY : clean
clean :
	$(RM) $(OBJGEN) $(OBJS) $(FILES)

